package org.shean.app.rssreader;

import org.shean.app.rssreader.provider.SzRSSReader;
import org.shean.app.rssreader.utils.ChannelSource;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class AddChannelActivity extends Activity {
	public EditText mURLText;
	public EditText mTitleText;
	
	protected ProgressDialog mBusy;
	final Handler mHandler = new Handler();
	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.channel_add);
		
		mURLText = (EditText) findViewById(R.id.url);
		mTitleText = (EditText) findViewById(R.id.name);
		
		Button add = (Button) findViewById(R.id.add);
		add.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {		
				Log.d("SZRSSReader","!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! add Channel!");
				addChannel();
			}
		});
		
		Button cancel = (Button) findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {		
				finish();
			}
		});
	}
	
	private void addChannel() {
		final String rssurl = mURLText.getText().toString();
		
		boolean isCustomTitle = false;
		
		if(mTitleText.getText() != null && mTitleText.getText().length() > 0)
		{
			isCustomTitle = true;
		}

		final String rssname = mTitleText.getText().toString();
		final boolean isSetRssName = isCustomTitle;
		
		mBusy = ProgressDialog.show(AddChannelActivity.this, "Download RSS",
				"Dowloading...", true, false);

		Thread t = new Thread() {
			public void run() {
				try {
					ChannelSource refresh = new ChannelSource(AddChannelActivity.this);

					final long id = refresh.syncDB(-1, rssurl);
					Log.d("RSS - Channel Add",
							"Running the channel add thread. ID: " + id);
					
					if(id >= 0 && isSetRssName)
					{
						Uri update = Uri.parse("content://" + SzRSSReader.AUTHORITY
								+ "/channels/" + id);
						ContentValues values = new ContentValues();
						values.put(SzRSSReader.Channels.TITLE, rssname);
						getContentResolver().update(update, values, null, null);
					}
					
					mHandler.post(new Runnable() {
						public void run() {
							mBusy.dismiss();

							Uri uri = ContentUris.withAppendedId(
									SzRSSReader.Channels.CONTENT_URI, id);
							getIntent().setData(uri);
							Log.d("RSS - ChannelAdd", "Uri value: "
									+ uri.toString());
							setResult(RESULT_OK, getIntent());
							finish();
						}
					});
				} catch (Exception e) {
					Log.d("::Exception::", e.toString());
					final String errmsg = e.getMessage();
					final String errmsgFull = e.toString();
					
					mHandler.post(new Runnable() {
						public void run() {
							mBusy.dismiss();
							
							Log.d("::Exception::","!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! create dialog!");
							String errstr = errmsgFull;
							/*
							if(errmsgFull.contains("java.net.ConnectException"))
								errstr = ChannelAdd.this.getString(R.string.error_net);
							else if(errmsgFull.contains("android.database.SQLException: Failed to insert row"))
								errstr = ChannelAdd.xthis.getString(R.string.error_exist_rss);
							else
								errstr = ChannelAdd.this.getString(R.string.error_rss_url);
							
							if(!isNetworkConnected())
								errstr = ChannelAdd.this.getString(R.string.error_no_net);
							*/
							
							new AlertDialog.Builder(AddChannelActivity.this).setTitle(
									"Error").setMessage(
											errstr).setPositiveButton("OK",
									new DialogInterface.OnClickListener() {
										// whatever.
										public void onClick(
												DialogInterface dialog,
												int whichButton) {

										}
									}).create().show();
						}
					});
				}
			}
		};

		t.start();
	}

}
